package com.hzt.agprosaleplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzt.agprosaleplatform.entity.Expert;
import com.hzt.agprosaleplatform.entity.User;
import com.hzt.agprosaleplatform.mapper.UserMapper;
import com.hzt.agprosaleplatform.service.RegisterLoginService;
import com.hzt.agprosaleplatform.mapper.ExpertMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegisterLoginServiceImpl implements RegisterLoginService {
    @Autowired
    UserMapper userMapper;
    
    @Autowired
    ExpertMapper expertMapper;

    @Override
    public User selectUserByUid(String uid){
        return userMapper.selectById(uid);
    }
    
    @Override
    public User selectUserByPhone(String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public void addUser(User user) {
        userMapper.insert(user);

        //专家注册
        if ("expert".equals(user.getRole())) {
            Expert expert = new Expert();
            expert.setUid(user.getUid());
            expert.setName(user.getRealname());

            expertMapper.insert(expert);
        }
    }
}
